package org.spiderdesign.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.spiderdesign.core.model.Employee;
import org.spiderdesign.core.model.vo.EmployeeVO;
import org.spiderdesign.core.service.EmployeeService;
import org.spiderdesign.model.JsonBean;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Resource
    private EmployeeService employeeService;

    @RequestMapping("/list")
    public IPage<EmployeeVO> list(@RequestParam(name = "page", defaultValue = "1") Integer page,
                                  @RequestParam(name = "limit", defaultValue = "1") Integer size) {
        return employeeService.selectEmployeePage(new Page<>(page, size));
    }

    @PostMapping("/add")
    public JsonBean<Void> add(@RequestParam String actualName,
                              @RequestParam String gender,
                              @RequestParam String loginName,
                              @RequestParam String password,
                              @RequestParam String phone,
                              @RequestParam(required = false) String email,
                              @RequestParam String department,
                              @RequestParam String position,
                              @RequestParam String idDisabled) {
        return employeeService.addEmployee(actualName, gender, loginName, password, phone, email, department, position, idDisabled);
    }

    // 获取员工详情
    @RequestMapping("/get")
    public JsonBean<Employee> get(@RequestParam String loginName) {
        return employeeService.getEmployeeByLoginName(loginName);
    }

    // 更新员工信息
    @PostMapping("/update")
    public JsonBean<Void> update(@RequestParam String loginName,
                                 @RequestParam String actualName,
                                 @RequestParam String gender,
                                 @RequestParam String phone,
                                 @RequestParam(required = false) String email,
                                 @RequestParam String department,
                                 @RequestParam String position,
                                 @RequestParam String idDisabled) {
        return employeeService.updateEmployee(loginName, actualName, gender, phone, email, department, position, idDisabled);
    }

    // 删除员工
    @PostMapping("/delete")
    public JsonBean<Void> delete(@RequestParam String loginName) {
        return employeeService.deleteEmployee(loginName);
    }

    // 批量删除员工
    @PostMapping("/batchDelete")
    public JsonBean<Void> batchDelete(@RequestBody List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            return new JsonBean<>(0, "请选择要删除的员工");
        }
        return employeeService.batchDeleteEmployees(ids);
    }

    @PostMapping("/changePassword")
    public JsonBean<Void> changePassword(@RequestParam String loginName,
                                         @RequestParam String newPassword) {
        if (loginName == null || loginName.trim().isEmpty()) {
            return new JsonBean<>(0, "登录账号不能为空");
        }
        if (newPassword == null || newPassword.trim().isEmpty()) {
            return new JsonBean<>(0, "新密码不能为空");
        }
        if (newPassword.length() < 6 || newPassword.length() > 12) {
            return new JsonBean<>(0, "密码长度必须在6-12位之间");
        }

        return employeeService.changeEmployeePassword(loginName, newPassword);
    }

    @RequestMapping("/verifyIdentity")
    public JsonBean<Void> verifyIdentity(String employeeUid) {
        return employeeService.verifyIdentity(employeeUid);
    }


}
